
class Box : public cyclone::CollisionBox
{
public:
    bool isOverlapping;


    Box()
    {
        body = new cyclone::RigidBody;
    }

    ~Box()
    {
        delete body;
    }

    /** Draws the box, excluding its shadow. */
    void render()
    {
        // Get the OpenGL transformation
        GLfloat mat[16];
        body->getGLTransform(mat);

        if (isOverlapping) glColor3f(1,0,0);
        else if (body->getAwake()) glColor3f(0.9f,0,0.9f);
        else glColor3f(0.3,0.3,0.3);

        glPushMatrix();
        glMultMatrixf(mat);
        glScalef(halfSize.x*2, halfSize.y*2, halfSize.z*2);
		glBegin(GL_QUADS);
		{
			glNormal3f( 0.0f, 0.0f, 1.0f);					// Normal Pointing Towards Viewer
			//glTexCoord2f(0.0f, 0.0f); 
			glVertex3f(-0.5f, -0.5f,  0.5f);	// Point 1 (Front)
			//glTexCoord2f(0.5f, 0.0f); 
			glVertex3f( 0.5f, -0.5f,  0.5f);	// Point 2 (Front)
			//glTexCoord2f(1.0f, 1.0f); 
			glVertex3f( 0.5f,  0.5f,  0.5f);	// Point 3 (Front)
			//glTexCoord2f(0.0f, 1.0f); 
			glVertex3f(-0.5f,  0.5f,  0.5f);	// Point 4 (Front)

			// Back Face
			glNormal3f( 0.0f, 0.0f,-1.0f);					// Normal Pointing Away From Viewer
			//glTexCoord2f(1.0f, 0.0f); 
			glVertex3f(-0.5f, -0.5f, -0.5f);	// Point 1 (Back)
			//glTexCoord2f(1.0f, 1.0f); 
			glVertex3f(-0.5f,  0.5f, -0.5f);	// Point 2 (Back)
			//glTexCoord2f(0.0f, 1.0f); 
			glVertex3f( 0.5,  0.5f, -0.5f);	// Point 3 (Back)
			//glTexCoord2f(0.0f, 0.0f); 
			glVertex3f( 0.5f, -0.5f, -0.5f);	// Point 4 (Back)

			// Top Face
			glNormal3f( 0.0f, 1.0f, 0.0f);					// Normal Pointing Up
			//glTexCoord2f(0.0f, 1.0f); 
			glVertex3f(-0.5f,  0.5f, -0.5f);	// Point 1 (Top)
			//glTexCoord2f(0.0f, 0.0f); 
			glVertex3f(-0.5f,  0.5f,  0.5f);	// Point 2 (Top)
			//glTexCoord2f(1.0f, 0.0f); 
			glVertex3f( 0.5f,  0.5f,  0.5f);	// Point 3 (Top)
			//glTexCoord2f(1.0f, 1.0f); 
			glVertex3f( 0.5f,  0.5f, -0.5f);	// Point 4 (Top)
			// Bottom Face

			glNormal3f( 0.0f,-1.0f, 0.0f);					// Normal Pointing Down
			//glTexCoord2f(1.0f, 1.0f); 
			glVertex3f(-0.5f, -0.5f, -0.5f);	// Point 1 (Bottom)
			//glTexCoord2f(0.0f, 1.0f); 
			glVertex3f( 0.5f, -0.5f, -0.5f);	// Point 2 (Bottom)
			//glTexCoord2f(0.0f, 0.0f); 
			glVertex3f( 0.5f, -0.5f,  0.5f);	// Point 3 (Bottom)
			//glTexCoord2f(1.0f, 0.0f); 
			glVertex3f(-0.5f, -0.5f,  0.5f);	// Point 4 (Bottom)
			// Right face
			glNormal3f( 1.0f, 0.0f, 0.0f);					// Normal Pointing Right
			//glTexCoord2f(1.0f, 0.0f); 
			glVertex3f( 0.5f, -0.5f, -0.5f);	// Point 1 (Right)
			//glTexCoord2f(1.0f, 1.0f); 
			glVertex3f( 0.5f,  0.5f, -0.5f);	// Point 2 (Right)
			//glTexCoord2f(0.0f, 1.0f); 
			glVertex3f( 0.5f,  0.5f,  0.5f);	// Point 3 (Right)
			//glTexCoord2f(0.0f, 0.0f); 
			glVertex3f( 0.5f, -0.5f,  0.5f);	// Point 4 (Right)
			// Left Face
			glNormal3f(-1.0f, 0.0f, 0.0f);					// Normal Pointing Left
			//glTexCoord2f(0.0f, 0.0f); 
			glVertex3f(-0.5f, -0.5f, -0.5f);	// Point 1 (Left)
			//glTexCoord2f(1.0f, 0.0f); 
			glVertex3f(-0.5f, -0.5f,  0.5f);	// Point 2 (Left)
			//glTexCoord2f(1.0f, 1.0f); 
			glVertex3f(-0.5f,  0.5f,  0.5f);	// Point 3 (Left)
			//glTexCoord2f(0.0f, 1.0f);
			glVertex3f(-0.5f,  0.5f, -0.5f);	// Point 4 (Left)   
		}
		glEnd();
        glPopMatrix();
    }

    /** Draws the ground plane shadow for the box. */
    void renderShadow()
    {
        // Get the OpenGL transformation
        GLfloat mat[16];
        body->getGLTransform(mat);

        glPushMatrix();
        glScalef(1.0f, 0, 1.0f);
        glMultMatrixf(mat);
        glScalef(halfSize.x*2, halfSize.y*2, halfSize.z*2);
        glBegin(GL_QUADS);
		{
			glVertex3f(0.5, 0.5, 0.5);  
			glVertex3f(0.5, -0.5, 0.5);  
			glVertex3f(-0.5, -0.5, 0.5); 
			glVertex3f(-0.5, 0.5, 0.5);  

			glVertex3f(0.5, 0.5, 0.5);     
			glVertex3f(0.5, 0.5, -0.5);  
			glVertex3f(0.5, -0.5, -0.5); 
			glVertex3f(0.5, -0.5, 0.5);    
		
			glVertex3f(-0.5, 0.5, -0.5);  
			glVertex3f(0.5, 0.5, -0.5);   
			glVertex3f(0.5, -0.5, -0.5);  
			glVertex3f(-0.5, -0.5, -0.5); 
		
			glVertex3f(-0.5, 0.5, 0.5);     
			glVertex3f(-0.5, 0.5, -0.5);  
			glVertex3f(-0.5, -0.5, -0.5); 
			glVertex3f(-0.5, -0.5, 0.5);    
		
			glVertex3f(-0.5, -0.5, -0.5); 
			glVertex3f(0.5, -0.5, -0.5);  
			glVertex3f(0.5, -0.5, 0.5);     
			glVertex3f(-0.5, -0.5, 0.5);    
		
			glVertex3f(-0.5, 0.5, -0.5);  
			glVertex3f(0.5, 0.5, -0.5);   
			glVertex3f(0.5, 0.5, 0.5);      		
			glVertex3f(-0.5, 0.5, 0.5);     
		}
		glEnd();
        glPopMatrix();
    }

    /** Sets the box to a specific location. */
    void setState(const cyclone::Vector3 &position, 
                  const cyclone::Quaternion &orientation, 
                  const cyclone::Vector3 &extents,
                  const cyclone::Vector3 &velocity)
    {
        body->setPosition(position);
        body->setOrientation(orientation);
        body->setVelocity(velocity);
        body->setRotation(cyclone::Vector3(0,0,0));
        halfSize = extents;

        cyclone::real mass = halfSize.x * halfSize.y * halfSize.z * 8.0f;
        body->setMass(mass);

        cyclone::Matrix3 tensor;
        tensor.setBlockInertiaTensor(halfSize, mass);
        body->setInertiaTensor(tensor);

        body->setLinearDamping(0.95f);
        body->setAngularDamping(0.8f);
        body->clearAccumulators();
        body->setAcceleration(0,-10.0f,0);

        //body->setCanSleep(false);
        body->setAwake();

        body->calculateDerivedData();
    }

	void position(){
	
	}
    /** Positions the box at a random location. */
    void random(cyclone::Random *random)
    {
        const static cyclone::Vector3 minPos(-5, 5, -5);
        const static cyclone::Vector3 maxPos(5, 10, 5);
        const static cyclone::Vector3 minSize(0.5f, 0.5f, 0.5f);
        const static cyclone::Vector3 maxSize(4.5f, 1.5f, 1.5f);

        setState(
            random->randomVector(minPos, maxPos),
            random->randomQuaternion(),
            random->randomVector(minSize, maxSize),
            cyclone::Vector3()
            );
    }
};
